/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kingbull.recorder.utils;


import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.Context;
import ohos.bundle.IBundleManager;

/**
 * 权限申请 工具栏
 */
public class PermissionUtils {

    private static boolean hasPermission(Context context, String permission) {
        return context.verifySelfPermission(permission) == IBundleManager.PERMISSION_GRANTED;
    }

    /**
     * 权限判断
     *
     * @param context     Context
     * @param permissions 权限字符串[]
     * @return boolean
     */
    public static boolean hasPermissions(Context context, String[] permissions) {
        boolean result = true;
        for (String permission : permissions) {
            if (!hasPermission(context, permission)) {
                result = false;
                break;
            }
        }

        return result;
    }

    /**
     * 权限判断 及申请
     *
     * @param ability Ability
     * @param permissions  权限字符串[]
     * @param requestCode int
     * @return boolean
     */
    public static boolean checkPermission(Ability ability, String[] permissions, int requestCode) {
        if (hasPermissions(ability, permissions)) {
            return true;
        } else {
            ability.requestPermissionsFromUser(permissions, requestCode);
            return false;
        }
    }

    /**
     * Checks all given permissions have been granted.
     *
     * @param grantResults results
     * @return returns true if all permissions have been granted.
     */
    public static boolean isAllGranted(int[] grantResults) {

        boolean result = true;
        for (int grantResult : grantResults) {
            if (grantResult != IBundleManager.PERMISSION_GRANTED) {
                result = false;
                break;
            }
        }
        return result;
    }

    /**
     * 获取权限
     *
     * @param permissions 权限字符串
     * @param ability     Ability
     */
    public static void getCameraPermission(String[] permissions, Ability ability) {
        boolean isDenied = false;
        String needPermission = "";
        for (String permission : permissions) {
            if (ability.verifySelfPermission(permission) != IBundleManager.PERMISSION_GRANTED) {
                isDenied = true;
                needPermission = permission;
                break;
            }
        }
        if (isDenied) {
            askPermission(needPermission, permissions, ability);
        }
    }

    private static void askPermission(String permission, String[] permissions, Ability ability) {
        if (ability.canRequestPermission(permission)) {
            // 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示)
            ability.requestPermissionsFromUser(permissions, 888);
        } else {
            // 进入设备设置界面启动
            Operation operation = new Intent.OperationBuilder()
                    .withAction("android.settings.SETTINGS")
                    .build();
            Intent intent = new Intent();
            intent.setOperation(operation);
            intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
            ability.startAbility(intent);
        }
    }

}
